//
//                       _oo0oo_
//                      o8888888o
//                      88" . "88
//                      (| -_- |)
//                      0\  =  /0
//                    ___/`---'\___
//                  .' \\|     |// '.
//                 / \\|||  :  |||// \
//                / _||||| -:- |||||- \
//               |   | \\\  -  /// |   |
//               | \_|  ''\---/''  |_/ |
//               \  .-\__  '-'  ___/-. /
//             ___'. .'  /--.--\  `. .'___
//          ."" '<  `.___\_<|>_/___.' >' "".
//         | | :  `- \`.;`\ _ /`;.`/ - ` : | |
//         \  \ `_.   \_ __\ /__ _/   .-` /  /
//     =====`-.____`.___ \_____/___.-`___.-'=====
//                       `=---='
//
//
//     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
//               佛祖保佑         永无BUG
//
//
//                          @王帅
package com.basics.controller;

import com.alibaba.fastjson.JSON;
import com.basics.dto.CuxToDoItems;
import com.basics.dto.CuxUsers;
import com.basics.service.IUserService;
import com.basics.service.ToDoItemsService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * 待办事项controller层
 *
 * @author shuaiwang02@hand-china.com
 */
@Controller
@RequestMapping("/items")
public class ItemsController {

    @Resource
    private ToDoItemsService toDoItemsService;


    /**
     * 根据用户ID查询本id的代办事项列表.
     * @return 返回对应页面
     */
    @RequestMapping("/listItems")
    @ResponseBody
    public List<CuxToDoItems> listItems(HttpServletRequest request, HttpServletResponse response) {

        HttpSession session = request.getSession();
        CuxUsers loggedUser = (CuxUsers) session.getAttribute("SESSION_USER");
        List<CuxToDoItems> items = this.toDoItemsService.listToDoItems(loggedUser.getUserId());


        return items;

    }

    /**
     * 根据待办事项ID更新待办事项.
     * 如前台传回的值有todoItmesId则调用修改 否则调用增加
     * @return 返回对应页面
     */
    @RequestMapping("/updateItems")
    @ResponseBody
    public Integer updateItems(HttpServletRequest request, HttpServletResponse response) {

        HttpSession session = request.getSession();
        CuxUsers loggedUser = (CuxUsers) session.getAttribute("SESSION_USER");

        Integer num = 0;
        CuxToDoItems items = new CuxToDoItems();
        items.setUserId(loggedUser.getUserId());
        items.setTodoItemTitle(request.getParameter("update_todoItemTitle"));
        items.setTodoItemContent(request.getParameter("update_todoItemContent"));
        items.setPriority(request.getParameter("update_priority"));
        items.setComments(request.getParameter("update_comments"));
        String a = request.getParameter("update_todoItemId");
        if (!a.equals("deficiency")) {
            items.setTodoItemId(Long.valueOf(request.getParameter("update_todoItemId")));
            num = toDoItemsService.updateItems(items);
        } else {
            num = toDoItemsService.insertItems(items);
        }
        return num;

    }

    /**
     * 根据items的id删除待办事项
     * @return 返回对应页面
     */
    @RequestMapping("/deleteItems")
    @ResponseBody
    public Integer deleteItems(HttpServletRequest request, HttpServletResponse response) {

        String dataList = request.getParameter("dataList");
        List<Integer> indexList = JSON.parseArray(dataList, Integer.class);

        Integer num = 0;
        for (int i = 0; i < indexList.size(); i++) {
            int result = toDoItemsService.deleteItems(indexList.get(i));
            num = num + result;
        }

        if (num.equals(indexList.size())){
            return 1;
        }else {
            return 0;
        }


    }


}